import java.util.ArrayList;
import java.util.List;

public class Fingerprint {

    // DO NOT write FFT.WINDOW_SIZE / 44100, it equals to 0 forever!!
    public static final double scaling = FFT.WINDOW_SIZE / 44100.0;

    public static final int N = 3;

    private ArrayList<int[]> constel_data = new ArrayList<>();
    private int id;

    /**
     * For songs about to add into DB
     * @param id
     */
    public Fingerprint(int id) {
        this.id = id;
    }



    /**
     * For songs about to be searched
     */
    public Fingerprint() {
        this.id = -1;
    }

    /**
     * Append a column of frequency peaks to the constellation map.
     * A frequency peak is a frequency value whose amplitude is the highest among
     * all frequencies in a frequency interval.
     *
     * @param freqDomain The frequency domain generated by FFT.
     */
    public void append(double[] freqDomain) {

        Heap heap = new Heap(N);
        for (int i = 0; i < freqDomain.length; ++i) {
            heap.insert(freqDomain[i], i);
        }

        int[] freqPeaks = heap.getValue();

        constel_data.add(freqPeaks);
    }

    /**
     * Generate fingerprints using Combinational Hash.
     * For each frequency peak, generate 6 fingerprints with its 6 successors.
     *
     * @return
     */
    public ArrayList<ShazamHash> combineHash() {
        if (constel_data.size() < 3)
            throw new RuntimeException("Too few frequency peaks");
        ArrayList<ShazamHash> hashes = new ArrayList<>();
        for (int i = 0; i < constel_data.size() - 2; ++i) {
            for (int k = 0; k < N; ++k) {

                // "Combine" with all peak frequencies inside its next two frames.
                for (int j = 1; j <= 2; ++j) {
                    for (int kk = 1; kk < N; ++kk) {
                        ShazamHash hash = new ShazamHash(
                                (short) constel_data.get(i)[k],
                                (short) constel_data.get(i + j)[kk],
                                (short) j,
                                i,
                                id);
                        hashes.add(hash);
                    }
                }
            }
        }
        return hashes;
    }
}
